import COMPONENTS.twChangeDeliveryStatus;
import COMPONENTS.twHelp;

import ENUMS.SlotStatus;

import EVENTS.CloseWithReasonEvent;
import EVENTS.SlotStatusChangedEvent;
import EVENTS.WaiterSelectedEvent;

import flash.ui.Keyboard;
import flash.utils.setTimeout;

import mx.controls.Alert;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.Areas;
import org.flexlancer.databases.Customers;


public var QTData:Object;
[Bindable]
public var dpAreas:Array;
public var sel_item_of_slot:XML;

public function set MenuFile(val:String):void{
	this.menucontrolMain.MenuFile = val;
}
public function set slotid(val:Number):void{
	this.menucontrolMain.slotID = val;
}

public function set cat(val:String):void{
	this.menucontrolMain.billCat = val;
}

public function set server(val:String):void{
	this.menucontrolMain.server = val;
}

private var assign_title:String ; 
public var kitchen_time:String = "";

public function cmbArea_OnChange():void{
	try{
		if ( this.cmbArea.selectedItem != null ){
			this.menucontrolMain.delivery_charges = this.cmbArea.selectedItem.charges;
		}
		//this.menucontrolMain.txtDeliveryCharges.text = this.cmbArea.selectedItem.charges;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
public function UpdateTitle():void{
	try{
		if ( this.kitchen_time == "" ){
			var dtNow:Date = new Date;
			this.kitchen_time = "["+dtNow.toLocaleTimeString()+"]";
			this.title = this.assign_title + " " + this.kitchen_time; 
		} 
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnCC():void{
	try{
		this.setFocus();
		this.menucontrolMain.currentState="DeliveryState";
		this.menucontrolMain.addEventListener(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,OnCloseWithStatus);
		this.menucontrolMain.addEventListener(MenuControl.REPORT_ME,OnReportMe);
		this.dpAreas = Areas.List();
		this.dpAreas.unshift({areaname:"<EMPTY>"});
		this.assign_title = this.title;
		
		if ( this.QTData != null ){
			this.menucontrolMain.QTData = this.QTData.QTData;
			this.txtContactNumber.text = this.QTData.telephone;
			this.txtName.text = this.QTData.name;
			this.txtAddress.text = this.QTData.address;
			this.taNotes.text = this.QTData.note;
			
			var index:int = -1;
			if ( this.QTData.area != "" ){
				for ( var i:int = 0 ; i < this.dpAreas.length ; i ++ ){
					if ( this.QTData.area == this.dpAreas[i].areaname ){
						index = i;
						break;
					}
				} 
			}
			if ( this.QTData.kitchen_time != null ){
				this.kitchen_time = this.QTData.kitchen_time;
			}
			this.cmbArea.selectedIndex = index;
			this.menucontrolMain.delivery_charges = this.QTData.delivery_charges;
			this.menucontrolMain.lastQTNumber = this.QTData.lastQTNumber;
		}else{
			this.cmbArea.selectedItem = null;
		}
		this.title = this.assign_title + " " + this.kitchen_time; 
		this.txtContactNumber.setFocus();
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

public function CustomerInfo():Object{
	try{
		var obj:Object = new Object();
		obj.telephone = this.txtContactNumber.text;
		obj.name = this.txtName.text;
		obj.area = this.cmbArea.selectedItem == null ? "" : this.cmbArea.selectedItem.areaname;
		obj.address = this.txtAddress.text;
		obj.note = this.taNotes.text;
		return obj;
	}catch(err:Error){
		throw err;
	}
	return null;
}

private function OnReportMe(evt:Event):void{
	try{
		var sendData:Object = new Object();
		sendData.QTData = this.menucontrolMain.QTData;
		sendData.lastQTNumber = this.menucontrolMain.lastQTNumber;
		sendData.server = this.menucontrolMain.server;
		sendData.telephone = this.txtContactNumber.text;
		sendData.name = this.txtName.text;
		sendData.address = this.txtAddress.text;
		sendData.area = this.cmbArea.selectedItem == null ? "" : this.cmbArea.selectedItem.areaname ;
		sendData.note = this.taNotes.text;
		sendData.kitchen_time = this.kitchen_time;
		sendData.delivery_charges = this.cmbArea.selectedItem == null ? "" : this.cmbArea.selectedItem.charges ;
		this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.REPORTING,sendData));
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnCloseWithStatus(evt:SlotStatusChangedEvent):void{
	try{
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private function OnClose():void{
	try{
		if ( this.menucontrolMain.IsEmptyBill ){
			this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.FREE,null));
		}else{
			var sendData:Object = new Object();
			sendData.QTData = this.menucontrolMain.QTData;
			sendData.lastQTNumber = this.menucontrolMain.lastQTNumber;
			sendData.server = this.menucontrolMain.server;
			sendData.telephone = this.txtContactNumber.text;
			sendData.name = this.txtName.text;
			sendData.address = this.txtAddress.text;
			sendData.area = this.cmbArea.selectedItem == null ? "" : this.cmbArea.selectedItem.areaname ;
			sendData.note = this.taNotes.text;
			sendData.kitchen_time = this.kitchen_time;
			sendData.delivery_charges = this.cmbArea.selectedItem == null ? "" : this.cmbArea.selectedItem.charges ;
			this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.PENDING,sendData));
		}
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
private var twH:twHelp = null;

private function OnKeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.F1:
					this.twH = PopUpManager.createPopUp(this,twHelp,true) as twHelp;
					PopUpManager.centerPopUp(this.twH);
			break;
			case Keyboard.ESCAPE:
				this.OnClose();
				break;
			case Keyboard.CONTROL:
				this.menucontrolMain.txtCode.setFocus();
				break;
			case Keyboard.INSERT:
				this.txtContactNumber.setFocus();
				break;
			case Keyboard.F8:
				this.ChangeRider();
				break;
		}
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnDropBill():void{
	try{
		//Alert.show("Are you sure to drop the bill?","Drop bill confirmation",Alert.YES | Alert.NO,this,this.OnDropBillConfirmation);
		var dropres:twDropReason = PopUpManager.createPopUp(this,twDropReason,true) as twDropReason;
		dropres.addEventListener(CloseWithReasonEvent.DROP_WITH_REASON,OnDropBillConfirmation);
		PopUpManager.centerPopUp(dropres);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnDropBillConfirmation(evt:CloseWithReasonEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			this.BillDroped(evt.reason);
		}
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function BillDroped(dropReason:String):void{
	try{
		this.menucontrolMain.OnDropBill(dropReason);
		//this.dispatchEvent(new SlotStatusChangedEvent(SlotStatusChangedEvent.SLOT_STATUS_CHANGED,SlotStatus.FREE,this.menucontrolMain.QTData));
		PopUpManager.removePopUp(this);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}
public function SaveCustomer():Boolean{
	try{
		if ( this.txtContactNumber.text == "" ) {
			Alert.show("Please enter contact number");
			this.txtContactNumber.setFocus();
			return false;
		}
		if ( this.txtName.text == "" ) {
			Alert.show("Please enter customer name");
			this.txtName.setFocus();
			return false;
		}
		if ( this.txtAddress.text == "" ) {
			Alert.show("Please enter address");
			this.txtAddress.setFocus();
			return false;
		}
		if ( this.cmbArea.selectedItem == null ) {
			Alert.show("Please enter area");
			this.cmbArea.setFocus();
			return false;
		}
		
		var nums:Array = Customers.ContactNumbers("telephone LIKE '"+this.txtContactNumber.text+"'");
		if ( nums.length > 0 ) {
			Customers.Update(this.txtContactNumber.text,this.txtName.text,this.cmbArea.selectedItem.areaname,this.txtAddress.text);
		}else{
			Customers.Add(this.txtContactNumber.text,this.txtName.text,this.cmbArea.selectedItem.areaname,this.txtAddress.text);
		}
		return true;		
	}catch(err:Error){
		throw err;
	}
	return false;
}
private function OnSaveCustomer():void{
	try{
		if ( this.SaveCustomer() )
			Alert.show("Customer saved successfully");
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private var sel_rider:twChangeDeliveryStatus;
private function ChangeRider():void{
	try{
		this.sel_rider = PopUpManager.createPopUp(this,twChangeDeliveryStatus,true) as twChangeDeliveryStatus;
		this.sel_rider.addEventListener(WaiterSelectedEvent.WAITER_SELECTED,OnDeliveryRiderSelected);
		PopUpManager.centerPopUp(this.sel_rider);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function SetAfterFocus():void{
	this.menucontrolMain.txtAmountPaid.setFocus();
}
private function OnDeliveryRiderSelected(evt:WaiterSelectedEvent):void{
	try{
		this.server = evt.WaiterName;
		this.title = "Serving \"" + String(this.menucontrolMain.slotID) + "\" by \"" + evt.WaiterName+"\"";
		this.sel_item_of_slot.@waitername = evt.WaiterName;
		//this.menucontrolMain.setFocus();
		setTimeout(this.SetAfterFocus,200);
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function txtContactNumber_OnTextChanged():void{
	try{
		var nums:Array = Customers.List("telephone LIKE '"+this.txtContactNumber.text+"'");
		if ( nums.length > 0 ){
			this.txtName.text = nums[0].name;
			this.txtAddress.text = nums[0].address;
			var index:Number = -1;
			for ( var i:Number = 0 ; i < this.dpAreas.length ; i ++ ){
				if ( this.dpAreas[i].areaname == nums[0].area ){
					index = i;
					break;
				}
			}
			this.cmbArea.selectedIndex = index;
			this.cmbArea_OnChange();
		}else{
			this.txtName.text = "";
			this.txtAddress.text = "";
			this.cmbArea.selectedItem = null;
		}
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}

private function OnResetCustomer():void{
	try{
		this.txtContactNumber.text = "";
		this.txtAddress.text = "";
		this.txtName.text = "";
		this.cmbArea.selectedIndex = -1;
		this.cmbArea_OnChange();
	}catch(err:Error){
		Alert.show(err.message+"\n"+err.getStackTrace(),"EXCEPTION");
	}
}